﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace DotNET4Study
{
    class StreamStudy
    {
        public static void Execute()
        {
            //1.流（内存流，文件流，网络流）中保持的是byte数组，直接操作起来不是很方便
            //  因此，可以在前端加入“用户友好的”读写器StreamReader/StreamWriter, 
            //  其可以直接通过开发人员理解起来更容易的字符串的形式来操作。

            byte[] buffers = null;
            using (MemoryStream ms = new MemoryStream())
            {
                using (StreamWriter writer = new StreamWriter(ms))
                {
                    writer.Write("Hello world.");

                    writer.Flush();
                    ms.Flush();
                    ms.Position = 0;
                    int length = (int)ms.Length;
                    buffers = new byte[length];
                    ms.Read(buffers, 0, length);
                }
            }

            using (MemoryStream ms = new MemoryStream(buffers))
            {
                using (StreamReader reader = new StreamReader(ms))
                {
                    string result = reader.ReadToEnd();
                    Console.WriteLine(result);
                }
            }
        }
    }
}
